package com.kkb.controller;

import com.kkb.pojo.Team;
import com.kkb.vo.QueryVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("param")
public class ParamController {

    @RequestMapping("hello.do")
    public ModelAndView hello(){
        return new ModelAndView("hello");
    }

    /*1.直接从页面获取对应参数*/
    @RequestMapping("test1.do")
    public ModelAndView test1(Integer teamId,String teamName,String teamLocation){
        System.out.println("test01---------------");
        System.out.println(teamId);
        System.out.println(teamName);
        System.out.println(teamLocation);
        ModelAndView mv = new ModelAndView();
        mv.addObject("teamId",teamId);
        mv.addObject("teamName",teamName);
        mv.addObject("teamLocation",teamLocation);

        mv.setViewName("ok");

        return mv;
    }

    /*2.使用对象接收多个参数*/
    @RequestMapping("test2.do")
    public ModelAndView test2(Team team){
        System.out.println("test2------------");
        System.out.println(team);
        return new ModelAndView("ok");

    }


    /*
    3.请求参数和方法名称的参数不一致
    * 使用@RequestParam进行矫正，value属性表示请求中的参数名称
    * required属性表示参数是否是必须的：true:必须赋值，否则报出400错；false：可以不赋值， 结果就是null
    * */
    @RequestMapping("test3.do")
    public ModelAndView test3(@RequestParam(value = "teamId",required = false) Integer id,
                              @RequestParam(value = "teamName",required = true) String name,
                              @RequestParam(value = "location") String loc){
        System.out.println("test3--------------");
        System.out.println("id:"+id);
        System.out.println("name:"+name);
        System.out.println("loc:"+loc);

        return new ModelAndView("ok");
    }


    //4.使用HttpServletRequest 对象获取参数：跟原来的javaWeb项目中使用的方式是一样的
    @RequestMapping("test4.do")
    public ModelAndView test4(HttpServletRequest request){

        System.out.println("test4------------");
        //获取页面属性参数值
        String id = request.getParameter("teamId");
        String name = request.getParameter("teamName");
        String location = request.getParameter("location");

        System.out.println("id:"+id);
        System.out.println("name:"+name);
        System.out.println("location:"+location);
        return new ModelAndView("ok");
    }


    //5.直接使用URL地址传参
    /*通过注解助@PathVariable 可以将URL中占位符参数{xxx}绑定到处理器类的方法形参中@PathVariable(“xxx“)*/
    @RequestMapping("{id}/{name}/{loc}/test5.do")
    public ModelAndView test5(@PathVariable("id") Integer id,
                              @PathVariable("name") String name,
                              @PathVariable("loc") String location){
        System.out.println("test5--------------");
        System.out.println("id:"+id);
        System.out.println("name:"+name);
        System.out.println("location:"+location);

        return new ModelAndView("ok");
    }


    //6.获取日期类型的参数
    @RequestMapping("test6.do")
    public ModelAndView test6(Team team){
        System.out.println("test6--------------");
        System.out.println(team);
        return new ModelAndView("ok");

    }


    //7.获取数组类型的参数
    @RequestMapping("test7.do")
    public ModelAndView test7(String[] teamName,HttpServletRequest request){
        System.out.println("test7--------------");
        //1.通过传参数直接传数组
        for(String s : teamName) {
            System.out.println(s);
        }

        System.out.println("-----------------------------------");
        //2.通过页面元素获取
        String[] teams = request.getParameterValues("teamName");
        for(String team : teams) {
            System.out.println(team);
        }

        return new ModelAndView("ok");
    }


    //8.获取集合类型的参数
    @RequestMapping("test8.do")
    public ModelAndView test8(@RequestParam("teamName") List<String> nameList){
        System.out.println("test8---------------------");
        for(String s : nameList) {
            System.out.println(s);

        }
        return new ModelAndView("ok");
    }


    @RequestMapping("test9.do")
    public ModelAndView test9(QueryVO vo){
        System.out.println("test9-------------------");
        for(Team team : vo.getTeamList()) {
            System.out.println(team);

        }

        return new ModelAndView("ok");
    }
}
